package org.lzy.shop.viewmodel

import androidx.lifecycle.LiveData
import androidx.lifecycle.MutableLiveData
import androidx.lifecycle.viewModelScope
import kotlinx.coroutines.launch
import org.lzy.shop.base.BaseViewModel
import org.lzy.shop.network.ApiException
import org.lzy.shop.response.CouponResponse
import org.lzy.shop.response.HomeGoodAllResponse
import org.lzy.shop.response.HomeResponse
import org.lzy.shop.viewmodel.repository.HomeRepository
import org.lzy.shop.viewmodel.repository.lmpl.HomeRepositoryImpl

/**
 * 主页的VM层
 */
class HomeViewModel: BaseViewModel() {

    private val homeRepository: HomeRepository = HomeRepositoryImpl()

    val homeLiveData= MutableLiveData<HomeResponse?>()

    val homeErrorData =MutableLiveData<Int?>()
    val homeGoodLiveData = MutableLiveData<HomeGoodAllResponse?>()

    val couponData = MutableLiveData<CouponResponse?>()

    val couponReceiveData = MutableLiveData<String?>()

    val couponReceiveFailedData = MutableLiveData<String?>()



    fun getHomeGoods(): LiveData<HomeResponse?>{
        viewModelScope.launch {
            try {
                val response = homeRepository.getHomeData()
                if (response != null) {
                    homeLiveData.value = response
                } else {
                    throw ApiException(-1, "获取主页数据失败")
                }
            } catch (e: Exception) {
                e.printStackTrace()
                if (e is ApiException) {
                    throw e
                } else {
                    throw ApiException(-1, e.message ?: "获取主页数据失败")
                }
            }
        }

        return homeLiveData
    }

    fun getHomeGoodAll(): LiveData<HomeGoodAllResponse?> {
        viewModelScope.launch {
            try {
                val response = homeRepository.getHomeGoodsAll()
                if (response != null) {
                    homeGoodLiveData.value = response
                } else {
                    throw ApiException(-1, "获取全部商品数据失败")
                }
            } catch (e: Exception) {
                e.printStackTrace()
                if (e is ApiException) {
                    throw e
                } else {
                    throw ApiException(-1, e.message ?: "获取全部商品数据失败")
                }
            }
        }
        return homeGoodLiveData
    }

    fun getCoupon(): LiveData<CouponResponse?>{
        viewModelScope.launch {
            try {
                val response = homeRepository.getCoupon()
                if (response != null) {
                    couponData.value = response
                } else {
                    throw ApiException(-1, "获取优惠券数据失败")
                }
            } catch (e: Exception) {
                e.printStackTrace()
                if (e is ApiException) {
                    throw e
                } else {
                    throw ApiException(-1, e.message ?: "获取优惠券数据失败")
                }
            }
        }
        return couponData
    }

    fun couponReceive(couponId:Int?): LiveData<String?>{
        viewModelScope.launch {
            try {
                // 检查参数有效性
                if (couponId == null || couponId <= 0) {
                    couponReceiveFailedData.value = "优惠券ID无效"
                    return@launch
                }

                val response = homeRepository.couponReceive(couponId)
                if (response != null) {
                    couponReceiveData.value = response
                } else {
                    throw ApiException(-1, "领取优惠券失败")
                }
            } catch (e: Exception) {
                e.printStackTrace()
                if (e is ApiException) {
                    couponReceiveFailedData.value = e.message ?: "领取优惠券时发生错误"
                } else {
                    couponReceiveFailedData.value = e.message ?: "领取优惠券时发生错误"
                }
            }
        }
        return couponReceiveData
    }

}